﻿$(window).load(function () {
    var wireAccordion = function (accordions) {
        accordions.accordion({
            autoHeight: false,
            active: false,
            collapsible: true,
            header: '>div>div:even'
        });
    };

    $.fn.treeviewKeys = function () {
        var keys = []
            , url
            , start = $(this)
            , length
            , lastKey
        //this won't work if the selector is contextual and a selector was used...
            , parentNodes = start.parentsUntil('#site-explorer').filter('div[data-treeview-key]')
            , getKey = function (s) { return s.attr('data-treeview-key'); };

        if (parentNodes.length == 0) return false;

        parentNodes.each(function (i, e) {
            keys.push(getKey($(e)));
        });

        keys.reverse();

        length = keys.length;

        url = keys.slice(1, length - 1).join('');

        if (length > 1) {
            lastKey = keys[length - 1];

            if (lastKey[1] == ':') {
                url += lastKey.slice(2);
            }
            else {
                url += lastKey;
            }
        }

        return { siteAuthority: keys[0].slice(2), url: url };
    };

    wireAccordion($('#site-explorer,#site-explorer .explorer-accordion', '#bd'));

    $('.explorer-accordion>div>div[data-treeview-ajax=true]:not([data-treeview-loaded])').live('click', function (e) {
        var thisItem = $(this)
                , loading = thisItem.parent().find('>div.ui-accordion-content>div:not(.toolbar)');

        a1.throbber(loading);

        loading.load('/UrlManagement/SiteChildUrls', $(e.currentTarget).treeviewKeys(), function () {
            wireAccordion(loading.find('>div'));

            steve.wireNewContent(loading);

            thisItem.attr('data-treeview-loaded', true);
            thisItem.trigger('treeview-load');
        });
    });




    $('a.add-site-url').live('click', function () {
        var k = $(this).treeviewKeys()
                , url = '';

        if (k) url = encodeURIComponent('http://' + k.siteAuthority + k.url);

        window.location.href = '/UrlManagement/SiteUrlDetail/?url=' + url;
    });

    var searchHandler = function (event) {
        var uri = parseUri($('#search-url-input').val())
            , test = uri.authority + uri.relative
            , recurse = function (listItems, test) {
                listItems.each(function (index, element) {
                    var thisItem = $(this)
                        , site = thisItem.attr('data-treeview-key')
                        , newTest = test;

                    if (site[1] == ':') site = site.slice(2);

                    if (test.indexOf(site) !== 0) return;

                    newTest = newTest.slice(site.length);

                    if (newTest.length == 0) {
                        //found...
                        thisItem
                            .parents('div[data-treeview-key]')
                            .find('>div.ui-accordion-header')
                            .each(function (index, element) {
                                var elem = $(element);

                                elem.parent().parent().accordion('activate', elem);
                                elem.click();
                            });
                    }
                    else {
                        var ajaxHeader = thisItem.find('>div[data-treeview-ajax=true]:not([data-treeview-loaded])')
                            , childKeySelector = '>div.ui-accordion-content>div>div.explorer-accordion>div[data-treeview-key],>div.ui-accordion-content>div>ul>li[data-treeview-key]';

                        if (ajaxHeader.length > 0) {
                            ajaxHeader.one('treeview-load', function (event) {
                                recurse($(event.currentTarget).parent().find(childKeySelector), newTest);
                            }).trigger('click');
                        }
                        else {
                            recurse(thisItem.find(childKeySelector), newTest);
                        }
                    }
                });
            };

        recurse($('#site-explorer>div[data-treeview-key]'), test);
    };

    $('#search-url-btn').click(searchHandler);
    $('#search-url-input').keypress(function (event) {
        if (event.keyCode == '13') searchHandler(event);
    });

    $('#site-explorer').show();
});